package cetcbigdata.da.service.hainan;

import cetcbigdata.da.base.Base;
import cetcbigdata.da.service.hainan.util.AllInfo;
import com.alibaba.datax.common.plugin.RecordSender;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

/**
 * 政务公开
 * 免证办清单
 */

public class MianZhengBanList extends Base {
    @Override
    public void getData(RecordSender recordSender, List<Object> fieldsList) {

    }

    @Override
    public JSONObject getDataDemo() {
        try {
            return parseListPage();
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    private JSONObject parseListPage() {
        AllInfo allInfo = new AllInfo();
        int currentPage = 1;
        int pages = 0;
        while (currentPage != pages){
            String listUrl = "https://wssp.hainan.gov.cn/wssp3/heatTheme/getMatterByIdAndCode";
            JSONObject listHeader = new JSONObject();
            listHeader.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)");
            listHeader.put("Referer", "https://wssp.hainan.gov.cn/hnwt/mianzhengban_detail?qdCategory=mzb&title=%E5%85%8D%E8%AF%81%E5%8A%9E%E6%B8%85%E5%8D%95");

            JSONObject listParmas = new JSONObject();
            listParmas.put("deptid", "");
            listParmas.put("topicid", "mianzheng");
            listParmas.put("xzqhid", "ALL");
            listParmas.put("pageNum", currentPage);
            listParmas.put("pageSize", "10");
            listParmas.put("tasktype", "");
            listParmas.put("taskname", "");
            listParmas.put("issub", "1");

            String listStrResponse = postFormReq(listUrl,listHeader,listParmas);
            JSONObject listResponse = JSONObject.parseObject(listStrResponse);
            if (listResponse.getInteger("code") == 20000){
                for (Object o : listResponse.getJSONObject("data").getJSONArray("resultList")){
                    JSONObject reqData = (JSONObject) o;
                    allInfo.setId(reqData.getString("id"));
                    allInfo.setSourcekey(reqData.getString("sourcekey"));
                    allInfo.init();
                    JSONObject result;
                    try {
                        result = allInfo.getAllInfo();
                    } catch (Exception e) {
                        break;
                    }
                    System.out.println(currentPage+"\n"+result);//测试用代码
                    return processResult(
                            null,
                            null,
                            null,
                            allInfo.getResponce(),
                            result,
                            "海南省",
                            "政务公开-免征办清单"
                    );
                }
                //更新当前页
                currentPage++;
                //更新总页数
                pages = listResponse.getJSONObject("data").getJSONObject("pageMap").getInteger("pages") + 1;
            }
        }
        return null;
    }

    public static void main(String[] args) {
        MianZhengBanList mianZhengBanList = new MianZhengBanList();
        mianZhengBanList.getDataDemo();
    }
}
